package com.bnuz.vtl.service;

import com.bnuz.vtl.model.PublicNode;
import com.bnuz.vtl.repo.PrivateNodeRepo;
import com.bnuz.vtl.repo.PublicNodeRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PublicNodeService implements BaseService<PublicNode, String> {

    @Autowired
    private PrivateNodeRepo privateNodeRepo;
    @Autowired
    private PublicNodeRepo publicNodeRepo;

    @Override
    public PublicNode save(PublicNode publicNode) {
        return publicNodeRepo.save(publicNode);
    }

    @Override
    public void deleteById(String publicNodeId) {
        publicNodeRepo.deleteById(publicNodeId);
    }

    @Override
    public List<PublicNode> findAll() {
        return publicNodeRepo.findAll();
    }

    @Override
    public PublicNode findById(String publicNodeId) {
        return publicNodeRepo.findById(publicNodeId).orElse(null);
    }

    public PublicNode findById(String publicNodeId, int depth) {
        return publicNodeRepo.findById(publicNodeId, depth).orElse(null);
    }

    public PublicNode getParent(String publicNodeId) {
        PublicNode parent = publicNodeRepo.getParent(publicNodeId);
        String nodeId = null;
        if(parent == null){
            nodeId = publicNodeId;
        } else {
            nodeId = parent.getPublicNodeId();
        }
        return publicNodeRepo.findById(nodeId, -1).orElse(null);
    }
}
